import { useState, useEffect } from 'react';
import { getDictByCode } from '@/services/public';

const useEnum = (parentCode) => {
  const [dict, setDict] = useState([]);
  useEffect(() => {
    const fetchEnum = async (enumCode) => {
      try {
        const result = await getDictByCode(enumCode);
        const { code, data } = result;
        if (code === 0) {
          setDict(data);
        }
      } catch (e) {
        console.error(e);
      }
    };

    fetchEnum(parentCode);
  }, [parentCode]);

  return dict;
};

export default useEnum;
